package goci

import "sync"

var (
	gd        = map[string]*GoCI{}
	gdRWMutex sync.RWMutex
)

func InitGoCI(key string) *GoCI {
	gc := NewGoCI()
	gc.sessionid = key
	if key != "" {
		gdRWMutex.Lock()
		gd[key] = gc
		gdRWMutex.Unlock()
	}
	return gc
}

func Get(key string, emptyNew bool) *GoCI {
	var gc *GoCI
	if key != "" {
		gdRWMutex.RLock()
		gc = gd[key]
		gdRWMutex.RUnlock()
	}
	if emptyNew && gc == nil {
		gc = NewGoCI()
	}
	return gc
}

func Remove(key string) {
	if key != "" {
		gdRWMutex.Lock()
		delete(gd, key)
		gdRWMutex.Unlock()
	}
}
